package cn.sytton.taffe.framework.security.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Date;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 登录用户(UserDetails)
 *
 * @author skyrock
 */
public class LoginUser implements UserDetails {

    private String userId;            //用户ID
    private String userName;           //姓名
    private String userCode;           //编号
    private String loginName;          //登录名
    @JsonIgnore
    private String password;           //密码
    private String email;              //用户邮箱
    private String cellphone;          //手机号
    private String userStatus;         //用户状态
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date expireDate;           //账号过期日期
    private Set<String> userRoles;     //用户角色列表
    private String ip;                 //登录IP
    private String loginLocation;      //登录地点
    private String browser;            //浏览器类型
    private String os;                 //操作系统
    @JsonIgnore
    private Long loginTimeStamp;       //登录时间戳
    private final Map<String,String> extInfo = new HashMap<String, String>(); //用户扩展信息

    @JsonIgnore
    private String tokenId;             //令牌ID
    @JsonIgnore
    private Long tokenRefreshTimeStamp; //令牌刷新时间
    @JsonIgnore
    private Long tokenExpireTimeStamp;  //过期时间戳

    public LoginUser() {}

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserCode() {
        return userCode;
    }

    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCellphone() {
        return cellphone;
    }

    public void setCellphone(String cellphone) {
        this.cellphone = cellphone;
    }

    public String getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(String userStatus) {
        this.userStatus = userStatus;
    }

    public Date getExpireDate() {
        return expireDate;
    }

    public void setExpireDate(Date expireDate) {
        this.expireDate = expireDate;
    }

    public Set<String> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(Set<String> userRoles) {
        this.userRoles = userRoles;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getLoginLocation() {
        return loginLocation;
    }

    public void setLoginLocation(String loginLocation) {
        this.loginLocation = loginLocation;
    }

    public String getBrowser() {
        return browser;
    }

    public void setBrowser(String browser) {
        this.browser = browser;
    }

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    public Long getLoginTimeStamp() {
        return loginTimeStamp;
    }

    public void setLoginTimeStamp(Long loginTimeStamp) {
        this.loginTimeStamp = loginTimeStamp;
    }

    @JsonFormat
    public Date getLoginTime() {
        return new Date(getLoginTimeStamp());
    }

    public Map<String, String> getExtInfo() {
        return extInfo;
    }

    public String getTokenId() {
        return tokenId;
    }

    public void setTokenId(String tokenId) {
        this.tokenId = tokenId;
    }

    public Long getTokenRefreshTimeStamp() {
        return tokenRefreshTimeStamp;
    }

    public void setTokenRefreshTimeStamp(Long tokenRefreshTimeStamp) {
        this.tokenRefreshTimeStamp = tokenRefreshTimeStamp;
    }

    public Long getTokenExpireTimeStamp() {
        return tokenExpireTimeStamp;
    }

    public void setTokenExpireTimeStamp(Long tokenExpireTimeStamp) {
        this.tokenExpireTimeStamp = tokenExpireTimeStamp;
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        //这是给SpringSecurity框架用的
        return this.loginName;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
